import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mortgageCalculator/modal/Toast.dart';
import 'package:mortgageCalculator/page/result.dart';

class SyPage extends StatefulWidget {
  SyPage({Key key}) : super(key: key);

  @override
  _SyPageState createState() => _SyPageState();
}

class _SyPageState extends State<SyPage> {
  TextEditingController dkjeController = TextEditingController(text: '');
  TextEditingController dkysController = TextEditingController(text: '');
  TextEditingController lprController = TextEditingController(text: '4.65');
  TextEditingController addController = TextEditingController(text: '');

  ScrollController _listController = ScrollController();

  String dkje = ''; // 贷款金额
  String dkys = ''; // 贷款月数
  String lpr = ''; // 贷款LPR
  String add = ''; // 上浮或下降多少
  String dklv = '4.65'; // 利率

  // 列表函数
  renderList(obj) {
    double s;
    double l;
    return Container(
      width: MediaQuery.of(context).size.width,
      height: 50,
      margin: EdgeInsets.only(bottom: 15),
      decoration: new BoxDecoration(
        //设置四周圆角 角度
        borderRadius: BorderRadius.all(Radius.circular(4.0)),
        //设置四周边框
        border: new Border.all(width: 1, color: Colors.orangeAccent),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            width: 100,
            height: 50,
            color: Colors.amber,
            child: Center(
              child: Text(obj['label'],
                  style: TextStyle(
                      fontSize: 16, //设置字体大小
                      fontWeight: FontWeight.w500)),
            ),
          ),
          Expanded(
            child: Container(
              padding: EdgeInsets.only(left: 25),
              child: obj['styleFlag'] == true
                  ? Text(obj['label'] == '贷款利率' ? dklv : obj['content'])
                  : TextField(
                      inputFormatters: [
                        (obj['label'] == '贷款金额' || obj['label'] == '贷款月数')
                            ? WhitelistingTextInputFormatter.digitsOnly
                            : BlacklistingTextInputFormatter(RegExp("[a-z]")),
                      ],
                      controller: obj['controller'],
                      decoration: InputDecoration(
                        border: InputBorder.none, // 去掉下滑线
                        counterText: '',
                        hintText: '请填写数字类型',
                      ),
                      onChanged: (value) => {
                        if (obj['label'] == '贷款金额')
                          {
                            setState(() {
                              dkje = value;
                            })
                          }
                        else if (obj['label'] == '贷款月数')
                          {
                            setState(() {
                              dkys = value;
                            })
                          }
                        else if (obj['label'] == '最新LPR')
                          {
                            if (value != '')
                              {
                                setState(() {
                                  lpr = value;
                                }),
                                lprController.text = value,
                                l = double.parse(lprController.text),
                                if (addController.text != '')
                                  {
                                    setState(() {
                                      dklv =
                                          (double.parse(addController.text) + l)
                                              .toString();
                                    })
                                  }
                                else
                                  {
                                    setState(() {
                                      dklv = l.toString();
                                    })
                                  }
                              }
                            else
                              {
                                setState(() {
                                  dklv = '4.65';
                                })
                              }
                          }
                        else if (obj['label'] == '加点数值')
                          {
                            if (value != '')
                              {
                                s = double.parse(value) / 100,
                                setState(() {
                                  dklv = (double.parse(lprController.text) + s)
                                      .toString();
                                })
                              }
                            else
                              {
                                setState(() {
                                  dklv = '4.65';
                                })
                              }
                          }
                      },
                    ),
            ),
          ),
          obj['inputFlag'] == true
              ? Container()
              : Container(
                  width: 80,
                  height: 50,
                  color: Colors.amber,
                  child: Center(
                      child: Text(obj['danwei'],
                          style: TextStyle(
                              fontSize: 16, //设置字体大小
                              fontWeight: FontWeight.w500))),
                ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SingleChildScrollView(
      controller: _listController,
      child: Container(
        padding: EdgeInsets.only(top: 15, left: 15, right: 15),
        decoration: new BoxDecoration(
            gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
              Color(0xFF39AC76),
              Color(0xFF39AC76).withOpacity(0),
            ])),
        child: Column(
          children: [
            renderList({
              'inputFlag': true,
              'styleFlag': true,
              'label': '计算方式',
              'content': '按贷款金额计算',
              'danwei': '',
              'controller': ''
            }),
            renderList({
              'inputFlag': false,
              'styleFlag': false,
              'label': '贷款金额',
              'content': '',
              'danwei': '元',
              'controller': dkjeController
            }),
            renderList({
              'inputFlag': false,
              'styleFlag': false,
              'label': '贷款月数',
              'content': '',
              'danwei': '月',
              'controller': dkysController
            }),
            renderList({
              'inputFlag': true,
              'styleFlag': true,
              'label': '利率方式',
              'content': '按最新LPR利率计算',
              'danwei': '',
              'controller': ''
            }),
            renderList({
              'inputFlag': false,
              'styleFlag': false,
              'label': '最新LPR',
              'content': '4.65',
              'danwei': '%',
              'controller': lprController
            }),
            renderList({
              'inputFlag': false,
              'styleFlag': false,
              'label': '加点数值',
              'content': '',
              'danwei': '‱',
              'controller': addController
            }),
            renderList({
              'inputFlag': false,
              'styleFlag': true,
              'label': '贷款利率',
              'content': '4.65',
              'danwei': '%',
              'controller': ''
            }),
            Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
              RaisedButton(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(6)),
                child: new Text('计算',
                    style: TextStyle(
                      color: Colors.white, //设置字体颜色
                      fontSize: 16, //设置字体大小
                    )),
                color: Colors.orangeAccent,
                onPressed: () {
                  if (dkje == '') {
                    return Toast.toast(context, msg: '贷款金额不能为空');
                  }
                  if (dkys == '') {
                    return Toast.toast(context, msg: '贷款金额月数不能为空');
                  }

                  if (int.parse(dkys) > 360) {
                    return Toast.toast(context, msg: '贷款月数不能超过360个月');
                  }
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => ResultPage(
                          result: {'dkje': dkje, 'dkys': dkys, 'dklv': dklv}),
                    ),
                  );
                },
              ),
              RaisedButton(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(6)),
                child: new Text('重置',
                    style: TextStyle(
                      fontSize: 16, //设置字体大小
                    )),
                onPressed: () {
                  dkjeController.text = '';
                  dkje = '';
                  dkysController.text = '';
                  dkys = '';
                  lprController.text = '4.65';
                  lpr = '4.65';
                  addController.text = '';
                  setState(() {
                    dklv = '4.65';
                  });
                },
              )
            ])
          ],
        ),
      ),
    ));
  }
}
